<?php
declare (strict_types=1);

namespace app\course\model\live\admin;

use app\course\event\subscribes\video\admin\Video as VideoSubscribe;
use app\course\model\base\admin\Course as BaseCourse;
use think\facade\Event;
use think\model\concern\SoftDelete;

/**
 * 点播课程--后台管理
 */
class Live extends BaseCourse
{
    use SoftDelete;

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'                   => 'integer',
        'status'               => 'integer',
        'mhm_id'               => 'integer',
        'teacher_id'           => 'integer',
        'update_time'          => 'timestamp',
        'listing_time'         => 'timestamp',
        'fullcategorypath'     => 'json',
        'mhm_fullcategorypath' => 'json',
    ];
    /**
     * 全局查询范围
     * @var [type]
     */
    protected $globalScope = ['courseType', 'mhm'];

    /**
     * 定义查询设备类型范围条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeCourseType($query)
    {
        $query->partition('p2')->where('course_live_type', 1);
    }

    /**
     * 获取课程日志类型
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/16
     * @param $value
     * @param $data
     * @return string
     */
    public function getCourseLogTypeAttr($value, $data)
    {
        return '大班课';
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 如果不是平台时, 加入机构条件限制
        if (!request()->is_platform) {
            $query->where('mhm_id', request()->mhm_id);
        }
    }

    /**
     * 分类数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-07
     * @param    [type]                         $category [description]
     * @param    [type]                         $data     [description]
     */
    public function setMhmCategoryAttr($category, $data)
    {
        return $this->setCategoryAttr($category, $data);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @return   string
     */
    public static function onBeforeInsert($course)
    {
        // 点播课程类型
        $course->course_type = 2;
        $course->mhm_id      = $course->getAttr('mhm_id') ?: request()->mhm_id;
        // 小班课类型值
        $course->course_live_type = 1;
        $course->admin_user_id    = request()->admin_user_id;
        $course->status           = 1;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterInsert($course)
    {
        parent::onAfterInsert($course);
        if (!Event::hasListener('VideoCreated')) {
            Event::subscribe(VideoSubscribe::class);
        }
        Event::trigger('VideoCreated', $course);
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterUpdate($course)
    {
        parent::onAfterUpdate($course);
        if (!Event::hasListener('VideoUpdated')) {
            Event::subscribe(VideoSubscribe::class);
        }
        Event::trigger('VideoUpdated', $course);
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterDelete($course)
    {
        parent::onAfterDelete($course);
        if (!Event::hasListener('VideoDeleted')) {
            Event::subscribe(VideoSubscribe::class);
        }
        Event::trigger('VideoDeleted', $course);
    }
}
